package com.cenxi.ipdemo1.utils.excel;

import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.AbstractCellStyleStrategy;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.ss.usermodel.*;

import java.util.List;
import java.util.Map;

/**
 * 设置表头颜色/字体
 */
public class CustomHeadStyleStrategy extends AbstractCellStyleStrategy {

    private WriteCellStyle headWriteCellStyle;
    private List<WriteCellStyle> contentWriteCellStyleList;

    public CustomHeadStyleStrategy(){
        setCustomHeadCellStyle();
    }

    private void setCustomHeadCellStyle() {
        //表头格式信息
        WriteCellStyle headCellStyle = new WriteCellStyle();
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontHeightInPoints((short) 24);
        //字体加粗
        headWriteFont.setBold(true);
        headWriteFont.setFontName("宋体");
        headCellStyle.setWriteFont(headWriteFont);
        headCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        headCellStyle.setFillForegroundColor(IndexedColors.LIGHT_TURQUOISE.getIndex());

        //内容格式信息
        WriteCellStyle contentCellStyle = new WriteCellStyle();
        //设置换行
        contentCellStyle.setWrapped(true);
        WriteFont contentWriteFont = new WriteFont();
        //设置字体大小
        contentWriteFont.setFontHeightInPoints((short) 8);
        contentWriteFont.setFontName("宋体");
        contentCellStyle.setWriteFont(contentWriteFont);
        //设置居中对齐
        contentCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);

        this.headWriteCellStyle = headCellStyle;
        this.contentWriteCellStyleList = ListUtils.newArrayList(contentCellStyle);
    }

    @Override
    protected void setHeadCellStyle(CellWriteHandlerContext context) {
        if (stopProcessing(context) || headWriteCellStyle == null) {
            return;
        }
        WriteCellData<?> cellData = context.getFirstCellData();
        WriteCellStyle.merge(headWriteCellStyle, cellData.getOrCreateStyle());
    }

    @Override
    protected void setContentCellStyle(CellWriteHandlerContext context) {
        if (stopProcessing(context) || CollectionUtils.isEmpty(contentWriteCellStyleList)) {
            return;
        }
        WriteCellData<?> cellData = context.getFirstCellData();
        if (context.getRelativeRowIndex() == null || context.getRelativeRowIndex() <= 0) {
            WriteCellStyle.merge(contentWriteCellStyleList.get(0), cellData.getOrCreateStyle());
        } else {
            WriteCellStyle.merge(
                    contentWriteCellStyleList.get(context.getRelativeRowIndex() % contentWriteCellStyleList.size()),
                    cellData.getOrCreateStyle());
        }
    }

    protected boolean stopProcessing(CellWriteHandlerContext context) {
        return context.getFirstCellData() == null;
    }
}


